<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>权限管理</title>
<%@include file="static/common/common.jspf"%>
</head>
<body>
<div id="menuToolbar" style="padding:5px;height:auto;">
		<a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-add" plain="true" id="addButton" onclick="openMenuWin(1)">新增</a>
		<a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-edit" plain="true" id="updateButton" onclick="openMenuWin(2)">修改</a>
		<a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-remove" plain="true" id="deleteButton" onclick="batchDeleteMenu()">删除</a> 
</div>
<table id="menuGrid" class="easyui-datagrid" title="用户信息列表" style="width:700px;height:250px;"
					data-options="singleSelect:false,
							collapsible:true,
							url:'menu!getMenuAll',
							method:'get',
							fit:true,
							toolbar:'#menuToolbar',
							pagination:true,
							pageSize:5,
							pageList:[3,4,5,10,20],
							striped:true
					">
			<thead>
				<tr>
					<th data-options="field:'ck',checkbox:true"></th>
					<th data-options="field:'menuId',width:80,hidden:'hidden'">菜单ID</th>
					<th data-options="field:'menuName',width:100">名称</th>
					<th data-options="field:'menuUrl',width:100">URL</th>
					<th data-options="field:'menuType',width:100">类型</th>
					<th data-options="field:'menuRole',width:100">权限代码</th>
					<th data-options="field:'menuPid',width:80,hidden:'hidden'">菜单权限ID</th>
				</tr>
			</thead>
	</table>
	<!-- 新增或修改 -->
 <div id="menuDialog"  class="easyui-dialog" style="width:400px;height:0px;"
	data-options="iconCls:'icon-edit',resizable:true,modal:true,closed:true">
	<form id="menuForm" method="get">
		<div style="margin-left:20px;margin-bottom:20px;">
			<input id="menuId" type="hidden" name="menu.menuId">
		</div>
		<div style="margin-left:20px;margin-bottom:20px;">
        	<select id="menuPid" class="easyui-combobox" name="menu.menuPid" style="width:80%;height:40px;padding:12px;"
        			data-options="valueField:'menuId',editable:false,textField:'menuName',url:'menu!selectAllmenuPidList',label:'父亲名称:'">
        	</select>
        </div>
		
		<div style="margin-left:20px;margin-bottom:20px;">
	         	名称:<input id="menuName" class="easyui-textbox" name="menu.menuName" style="width:80%;height:40px;padding:12px;" 
        			data-options="prompt:'必填',required:true">
		</div>
		
		<div style="margin-left:20px;margin-bottom:20px;">
	   		URL:<input id="menuUrl" class="easyui-textbox" name="menu.menuUrl" style="width:80%;height:40px;padding:12px;" 
        			data-options="prompt:'跳转则需赋值,可为空'">
		</div>
		
		<div style="margin-left:20px;margin-bottom:20px;">
		<select id="menuType" class="easyui-combobox" name="menu.menuType" style="width:80%;height:40px;padding:12px;"
		data-options="valueField:'menuType',editable:false,textField:'menuType',url:'menu!selectAllmenuTypeList',label:'类型:'">
        </select>
		</div>
		
		<div style="margin-left:20px;margin-bottom:20px;">
	   		权限代码:<input id="menuRole" class="easyui-textbox" name="menu.menuRole"  style="width:80%;height:40px;padding:12px;" 
        			data-options="prompt:'权限设置代码,可为空'">
		</div>		
        
        <div style="margin-left:20px;margin-bottom:20px;">
        	<a href="#" class="easyui-linkbutton" data-options="iconCls:'icon-ok'" style="padding:5px 0px;width:30%;" onclick="submitForm()" >
        	<span style="font-size:14px;">提交</span>
        	</a>
        </div>
	</form>
</div>	
<script type="text/javascript">
var addUrl;
var updateUrl;
var deleteUrl;
$(function(){
	var mIDStr=4;
	$("#addButton").hide();
	$("#updateButton").hide();
	$("#deleteButton").hide();
	$.ajax({
		url:"user!buttonControl",
		type:"post",
		data:{
			menui:mIDStr
		},
		dataType:"json",
		success:function(data){
			$.each(data,function(index,item){
				if(item.menuName =='新增'){
					addUrl=item.menuRole;
					$("#addButton").show();
				}
				
				if(item.menuName=='编辑'){
					updateUrl=item.menuRole;
					$("#updateButton").show();
				}
				
				if(item.menuName=='删除'){
					deleteUrl=item.menuRole;
					$("#deleteButton").show();
				}
			});
		}
	})
	
})




var url;
function openMenuWin(flag){
	$("#menuForm").form("reset");
	//如果flag为1的时候则为新增,直接打开窗口
	//如果flag为2的时候则为修改,将选中的行数据赋给对应表单元素
	if(flag !=1){
		var rows=$("#menuGrid").datagrid("getChecked");
		if(rows.length !=1){
			winAlert("提示","请选择一行数据进行修改！");
		}else{
			//"menu!editMenu"
			url=updateUrl;
			var data=rows[0];
			
			$("#menuId").val(data.menuId);
			$("#menuPid").val(data.menuPid);
			$("#menuName").textbox('setValue',data.menuName);
			$("#menuUrl").textbox('setValue',data.menuUrl);
			$("#menuType").textbox('setValue',data.menuType);
			$("#menuRole").textbox('setValue',data.menuRole);
		}
	}else{
		//'menu!addMenu'
		url = addUrl;
	}
	$("#menuDialog").dialog('open');
} 

function submitForm(){
	$('#menuForm').form('submit', {
		url: url,
		success: function(result){
			var data = JSON.parse(result);
			if(data.isok == true){
				$("#menuGrid").datagrid('reload');
				winAlert("成功!", "操作成功!")
			}else{
				winAlert("错误!", "操作失败!")
			}
			$("#menuDialog").dialog('close');
		}
	});
}

	function batchDeleteMenu(){
		//获取所有选中的行
		var rows=$("#menuGrid").datagrid("getChecked");
		if(rows.length !=0){
			var menuIds;
			$.each(rows,function(index,item){
				if(index==0){
					menuIds=item.menuId;
				}else{
					menuIds=menuIds+","+item.menuId
				}
			});
			//'menu!batchDeleteMenu'
			 $.post(deleteUrl,{'menuIds':menuIds}, function(data){
				if(data.isok ==true){
					//局部刷新表
					$("#menuGrid").datagrid("reload");
					winAlert("提示","删除成功！");
					}else{
					//局部刷新表
					$("#menuGrid").datagrid("reload");
					winAlert("提示","删除失败！");
					}
			},'json'); 
			
			/* $.ajax({
				type:"post",
				url:"user!batchDeleteUser",
				data:{
					ids:ids
				},
				dataType:"json",
				traditional:true,
				success:function(data){
					if(data.isok ==true){
					//局部刷新表
					$("#userGrid").datagrid("reload");
					winAlert("提示","删除成功！");
					}else{
					//局部刷新表
					$("#userGrid").datagrid("reload");
					winAlert("提示","删除失败！");
					}
				}
			}) */
		}else{
			winAlert("提示","请至少选中一行进行删除！");
		}
	}
	
	function winAlert(title,msg){
		$.messager.show({
			title:title,
			msg:msg,
			showType:'fade',
			style:{
				right:'',
				bottom:''
			}
		})
	}
</script>
</body>
</html>